package org.jeecg.modules.music.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.entity.MusTopList;
import org.jeecg.modules.music.entity.MusListSong;
import org.jeecg.modules.music.entity.MusTopList;
import org.jeecg.modules.music.service.IMusTopListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Slf4j
@Api(tags="榜单管理")
@RestController
@RequestMapping("/mus/toplist")
public class MusTopListController {
    
    @Autowired
    private IMusTopListService musTopListService;

    @ApiOperation("列表")

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<MusTopList>> queryPageList(MusTopList topList, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<MusTopList>> result = new Result<IPage<MusTopList>>();
        QueryWrapper<MusTopList> queryWrapper = new QueryWrapper<>();
        if (oConvertUtils.isNotEmpty(topList.getToplistType())){
            queryWrapper.eq("toplist_type",topList.getToplistType());
        }
        if(oConvertUtils.isNotEmpty(topList.getName())){
            queryWrapper.and(wrapper ->wrapper.like("name",topList.getName()).or().like("description",topList.getDescription()));
        }
        Page<MusTopList> page = new Page<MusTopList>(pageNo, pageSize);
        IPage<MusTopList> pageList = musTopListService.page(page,queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }



    @ApiOperation("添加")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<MusTopList> add(@RequestBody JSONObject jsonObject){
        Result<MusTopList> result = new Result<MusTopList>();

        try{
            MusTopList topList = JSON.parseObject(jsonObject.toJSONString(),MusTopList.class);
            topList.setCreateTime(new Date());
            topList.setPlayCount(0);
            QueryWrapper<MusTopList> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name",topList.getName());
            MusTopList toplist = musTopListService.getOne(queryWrapper);
            if (oConvertUtils.isNotEmpty(toplist)){
                result.error500("改榜单已经存在");
                return result;
            }
            musTopListService.saveArtist(topList);
            result.success("添加成功！");
        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation("编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<MusTopList> edit(@RequestBody JSONObject jsonObject) {
        Result<MusTopList> result = new Result<MusTopList>();
        try {
            MusTopList info = musTopListService.getById(jsonObject.getString("id"));

            if(info==null) {
                result.error500("未找到对应实体");
            }else {
                MusTopList topList = JSON.parseObject(jsonObject.toJSONString(), MusTopList.class);
                topList.setUpdateTime(new Date());
                musTopListService.editTopList(topList);
                result.success("修改成功!");
            }
        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        this.musTopListService.deleteTopList(id);
        return Result.ok("删除成功");
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.musTopListService.deleteBatchTopList(ids);
        return Result.ok("批量删除用户成功");
    }

    @ApiOperation("榜单的曲目")
    @GetMapping("/songs")
    public Result<IPage<MusSong>> queryPageSongs(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 @RequestParam(name = "toplistId",required = false) String toplistId,
                                                 MusSong songParams){
        Result<IPage<MusSong>> result = new Result<IPage<MusSong>>();
        Page<MusSong> page = new Page<MusSong>(pageNo, pageSize);
        IPage<MusSong> pageList = musTopListService.querySongDetailPage(page,toplistId,songParams);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }


    @ApiOperation("获取更多歌曲")
    @GetMapping("/querySongs")
    public Result<IPage<MusSong>> queryNoPageSongs(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(name = "toplistId",required = false) String toplistId,
                                                   MusSong songParams){
        Result<IPage<MusSong>> result = new Result<IPage<MusSong>>();
        Page<MusSong> page = new Page<MusSong>(pageNo, pageSize);
        IPage<MusSong> pageList = musTopListService.queryNoSongDetailPage(page,toplistId,songParams);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @RequestMapping(value = "/addSongs", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<?> handleAddSongs(@RequestBody JSONObject jsonObject){

        this.musTopListService.saveSongs(jsonObject.getString("toplistId"),jsonObject.getString("songIds"));
        return Result.ok("批量添加歌曲成功");
    }

    @RequestMapping(value = "/deteleRelation", method = RequestMethod.DELETE)
    public Result<?> handleDeleteListSong(@RequestParam(name="toplistId",required=true) String toplistId,@RequestParam(name="songId",required=true)String songId){
        this.musTopListService.deleteTopListSong(toplistId,songId);
        return Result.ok("批量删除成功");
    }

    @RequestMapping(value = "/deleteBatchRelation", method = RequestMethod.DELETE)
    public Result<?> handleDeleteListSongs(@RequestParam(name="toplistId",required=true) String toplistId,@RequestParam(name="songIds",required=true)String songIds){
        this.musTopListService.deleteTopListSongBatch(toplistId,songIds);
        return Result.ok("批量删除成功");
    }
}
